<template>
	<view class="edit">
		<navbar ref="navbar" backColor="#000000" :config="config"></navbar>
		<form @submit="formSubmit">
			<view class="middle">
				<view class="itemHead">
					<view class="left">
						头像
					</view>
					<view class="right">
						<image v-if="unserInfo.head_photo" 
						style="width: 81rpx;height: 81rpx;border-radius: 50%;"
						:src="unserInfo.head_photo" @click="uploadImg"
							mode="aspectFill">
						</image>
						<!-- <image v-else :src="imgUrl+'/head-on.png'" mode="aspectFill" @click="uploadImg"></image> -->
						<image v-else :src="imgUrl+'/personal/head.png'" class="logoImage"
							style="width: 81rpx;height: 81rpx;border-radius: 50%;" @click="uploadImg"></image>
						<image :src="imgUrl+'/set/right.png'" class="name-rightImage" ></image>
					</view>
				</view>
				<view class="name-item">
					<view class="name-left">
						姓名
					</view>
					<view class="name-right">
						<input type="text" :value="unserInfo.real_name" 
						style="color: #999999;"
						maxlength="32" @input="onName" class="my-name"
							placeholder="请输入姓名" placeholder-class="placeClass" />
						<image :src="imgUrl+'/set/right.png'"></image>
					</view>
				</view>
				<view class="item">
					<view class="left">
						手机号
					</view>
					<view class="right">
						<view class="phone">
							{{unserInfo.mobile}}
						</view>
					</view>
				</view>
				<view class="item">
					<view class="left">
						性别
					</view>
					<view class="right" v-if="platform.length>0">
						<picker class="choice" @change="bindPickerChange" :value="unserInfo.gender" :range="platform"
							range-key="name">
							<view class="uni-input">{{platform[index].name}}
								<!-- <text class="iconfont">&#xe6c7;</text> -->
							</view>
						</picker>
					</view>
				</view>
				<view class="item">
					<view class="left">
						生日
					</view>
					<view class="right" style="flex: 1;">
						<picker style="flex: 1;height: 38rpx;text-align: right;" mode="date" :value="date"
							:start="startDate" :end="endDate" @change="bindDateChange">
							<view class="uni-input">{{date ? date : '未知'}}</view>
						</picker>
						<image :src="imgUrl+'/set/right.png'" class="name-rightImage"></image>

					</view>
				</view>
				<!-- <view class="item">
					<view class="left">
						等级
					</view>
					<view class="right">
						<view class="name">
							{{unserInfo.role_name}}
						</view>
					</view>
				</view> -->

			</view>

			<view class="submit-top">
				<button class="submit" form-type="submit">
					保存修改
				</button>
			</view>
		</form>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		if (type === 'start') {
			year = year - 150;
		} else if (type === 'end') {
			year = year + 150;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	}
	import {
		appTheeLbs
	} from "@/api/basic.js"
	import {
		myInfo,
		updateInfo
	} from "@/api/login.js"
	export default {
		data() {
			return {
				config: {
					back: true, //false是tolbar页面 是则不写
					title: '修改资料',
					color: '#000',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, "#fff"],
					statusBarFontColor: '#1A1A1A',
					backIconColor: "#000"
				},
				platform: [{
						name: '女'
					},
					{
						name: '男'
					},
					{
						name: "保密"
					},
				],
				imgSize: 4,
				index: 0, //性别
				date: getDate({
					format: true
				}),
				// date: '',
				startDate: getDate('start'),
				endDate: getDate('end'),
				time: '12:01',
				newProvinceDataList: [
					[],
					[],
					[]
				],
				unserInfo: {},
				ossData: null,
				imgType: ['jpeg', 'png', 'jpg'],
				imgUrl: this.$mConfig.staticUrl
			}
		},
		//组件


		//获得省市区
		onShow() {
			this.$http.get(appTheeLbs).then(res => {
				let provincesList = res.tree.children
				for (let i = 0; i < provincesList.length; i++) {
					this.newProvinceDataList[0].push(provincesList[i]);

				}
			})
			//取shop的值
			let shop = uni.getStorageSync("shop")

		},
		onLoad() {
			this.getUserInfo();
			// this.formDataGet();
		},
		methods: {

			//上传凭证
			formDataGet() {
				let that = this
				uni.request({
					//url: 'https://pm.lidashine.com/get-oss-sign', //oss获取参数接口地址。
					// url: this.$mConfig.baseUrl + '/fileMgr/v1/oss/getOssSign',
					url: 'http://192.168.0.101:9020/fileMgr/v1/oss/getOssSign',
					complete: (result) => {
						console.log(result, 'result')
						let res = JSON.parse(result.data.data);
						console.log('res===>', res)
						that.ossData = res
					}
				})
			},
			//获取登录的用户信息
			getUserInfo() {
				this.$http.get(myInfo).then(res => {
					this.index = res.data.gender //男女
					this.date = res.data.birthday;
					this.unserInfo = res.data;

				})
			},



			//修改个人信息
			formSubmit(e) {
				let target = e.detail.value
				let mobile = this.unserInfo.mobile
				let gender = this.index.toString()
				let param = {
					real_name: this.unserInfo.real_name,
					head_photo: this.unserInfo.head_photo, //this.unserInfo.head_photo, //图像
					gender: gender, //性别
					birthday: this.date, //生日
				}
				this.$http.put(updateInfo, param)
					.then(res => {

						if (res.code == 200) {
							this.$mUtil.toast("修改成功")
							let aa = uni.getStorageSync("personal", res.data)

						} else {
							this.$mUtil.toast("修改失败")
						}
					})


			},
			//更换图片
			updataImg(filePath, nameStr) {
				console.log(filePath, nameStr)
				let date = new Date();
				let that = this
				let allData = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate()
				let shopid = uni.getStorageSync('shop')
				let id = 0
				if (!shopid) {
					id = 0
				} else {
					id = shopid.id
				}
				let key = 'img/' + id + '/' + this.type + '/' + allData + '/' + nameStr
				uni.uploadFile({
					// url:  this.$mConfig.baseUrl+ '/minio/upload',
					url: this.$mConfig.baseUrl + '/sys/oss/upload',
					filePath: filePath,
					fileType: 'image',
					name: 'file',
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode != 200) {
							return;
						}
						let resObj = JSON.parse(res.data);
						let imgurl = resObj.data.url;

						uni.showToast({
							title: '上传成功',
							icon: 'success',
							duration: 1000
						})
						that.unserInfo.head_photo = imgurl
						// alert(ossRes.host+"/"+encodeURIComponent(fileName));

					},
					fail: (err) => {
						uni.hideLoading();

						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					}
				});
			},

			//更换图片
			uploadImg() {
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						let that = this
						if (res) {
							let filePath = res.tempFilePaths[0]

							let nameStr = that.random_string(8) + '.' + res.tempFilePaths[0].split('.').pop();
							if (res.tempFiles) {
								for (let item of res.tempFiles) {
									if (item.size > (that.imgSize * 1024 * 1024)) {
										uni.showToast({
											title: `图片不能大于${that.imgSize}M`,
											icon: 'none'
										})
										return false;
									}
									if (item.type) {
										let r = that.imgType.some(v => {
											let type = item.type.split('/');
											if (type.length)
												return (v === type[1]);
										});
										if (!r) {
											uni.showToast({
												title: `只允许上传${that.imgType}的类型`,
												icon: 'none'
											})
											return false;
										}
									}
								}
							}
							console.log("上传图片",res)


							that.updataImg(filePath, nameStr)
						}

					},fail(e){
						console.log("e",e)
					}
				});
			},
			random_string(len) {
				len = len || 32;
				var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
				var maxPos = chars.length;
				var pwd = '';
				for (let i = 0; i < len; i++) {
					pwd += chars.charAt(Math.floor(Math.random() * maxPos));
				}
				return pwd;
			},

			//修改昵称
			onName(e) {
				this.unserInfo.real_name = e.detail.value
			},
			bindPickerChange: function(e) {

				this.index = e.detail.value
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
		}

	}
</script>

<style scoped lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.name-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 85rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 0 38rpx;
		box-sizing: border-box;

		.name-left {
			font-weight: 400 !important;
			font-size: 27rpx !important;
			color: #333333 !important;
			font-weight: bold !important;
		}

	}

	.submit-top {
		width: 100%;
		padding: 0 30rpx;

		position: fixed;
		bottom: 103rpx;



	}

	.submit {
		height: 88rpx;
		background: #FF211F;
		border-radius: 19rpx 19rpx 19rpx 19rpx;
		font-weight: 400;
		font-size: 29rpx;
		color: #FFFFFF;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.distance {
		height: 10rpx;
		background-color: #F5F5F5;
	}

	.middle {
		

		.item {


			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			height: 85rpx;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			padding: 0 38rpx;
			box-sizing: border-box;

			

			.right {

				// flex: 1;
				text-align: right;
				display: flex;
				align-items: center;
				font-size: 27rpx;
				color: #999999;
				line-height: 38rpx;

				.my-phone {
					text-align: right;
				}

				.my-name {
					text-align: right;
				}

				.name {
					font-size: 28rpx;
					color: #474747;
					// font-weight: 400;

				}

				image {
					width: 76rpx;
					height: 76rpx;
					border-radius: 50%;
				}

				.iconfont {
					margin-left: 15rpx;
				}
			}
		}
	}

	.name-rightImage {
		width: 12rpx !important;
		height: 23rpx !important;
		border-radius: 0rpx 0rpx 0rpx 0rpx !important;
		margin-left: 19rpx !important;
	}

	.rightImage {
		width: 12rpx;
		height: 23rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		margin-left: 19rpx;
	}

	.name-right {
		flex: 1;
		text-align: right;
		display: flex;
		align-items: center;

		.placeClass {
			font-weight: 400;
			font-size: 27rpx;
			color: #999999;
		}

		.my-name {
			width: 100%;
		}

		image {
			width: 12rpx;
			height: 23rpx;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			margin-left: 19rpx;
		}
	}
	.itemHead {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 38rpx;
		box-sizing: border-box;
		width: 750rpx;
		height: 131rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 0 38rpx;
		box-sizing: border-box;
		
	}
	.logoImage{
		width: 83rpx;
		height: 83rpx;
		border-radius: 50%;
	}
	.right{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}
	.left {
		font-weight: 400 !important;
		font-size: 27rpx !important;
		color: #333333 !important;
		font-weight: bold !important;
	}
</style>